{% extends "rbac/base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <h2>账户信息</h2>
            
            {% if messages %}
                {% for message in messages %}
                    <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="close" data-dismiss="alert">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                {% endfor %}
            {% endif %}
            
            <div class="card">
                <div class="card-header">
                    <h5>个人信息</h5>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}
                        
                        <div class="form-group row">
                            <label for="username" class="col-sm-3 col-form-label">用户名</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="username" value="{{ user_info.username }}" readonly>
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <label for="email" class="col-sm-3 col-form-label">邮箱</label>
                            <div class="col-sm-9">
                                <input type="email" class="form-control" id="email" value="{{ user_info.email }}" readonly>
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <label for="first_name" class="col-sm-3 col-form-label">名</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="first_name" name="first_name" 
                                       value="{{ user_info.first_name }}" placeholder="请输入您的名字">
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <label for="last_name" class="col-sm-3 col-form-label">姓</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="last_name" name="last_name" 
                                       value="{{ user_info.last_name }}" placeholder="请输入您的姓氏">
                            </div>
                        </div>
                        
                        <hr>
                        
                        <h6>修改密码</h6>
                        <div class="form-group row">
                            <label for="old_password" class="col-sm-3 col-form-label">原密码</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="old_password" name="old_password" 
                                       placeholder="如需修改密码，请输入原密码">
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <label for="new_password" class="col-sm-3 col-form-label">新密码</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="new_password" name="new_password" 
                                       placeholder="请输入新密码">
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <label for="confirm_password" class="col-sm-3 col-form-label">确认密码</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" id="confirm_password" name="confirm_password" 
                                       placeholder="请再次输入新密码">
                            </div>
                        </div>
                        
                        <div class="form-group row">
                            <div class="col-sm-9 offset-sm-3">
                                <button type="submit" class="btn btn-primary">更新信息</button>
                                <a href="{% url 'rbac:dashboard' %}" class="btn btn-secondary">返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const newPassword = document.getElementById('new_password');
    const confirmPassword = document.getElementById('confirm_password');
    const oldPassword = document.getElementById('old_password');
    
    function validatePasswords() {
        if (newPassword.value && newPassword.value !== confirmPassword.value) {
            confirmPassword.setCustomValidity('密码不匹配');
        } else {
            confirmPassword.setCustomValidity('');
        }
        
        if (newPassword.value && !oldPassword.value) {
            oldPassword.setCustomValidity('修改密码时必须输入原密码');
        } else {
            oldPassword.setCustomValidity('');
        }
    }
    
    newPassword.addEventListener('input', validatePasswords);
    confirmPassword.addEventListener('input', validatePasswords);
    oldPassword.addEventListener('input', validatePasswords);
});
</script>
{% endblock %}